<?php
/**
 * User Class Model
 *
 * Copyright 2012
 * @date 21-April-2012
 * @author 
 * @version 0.0.1
 **/
class User extends AppModel {
/**
 * Name
 *
 * @var string
 */
    public $name = 'User';
    
    const ADMINISTRATOR = 1;
    const MOD = 2;


    
    public $belongsTo = array(
        'Group'=>array(
            'className'=>'Group',
            'foreignKey'=>'group_id',
            'conditions'=>array('role_id'=>self::MOD )
        )
    );
    
/**
 * Validation rules
 *
 * @var string
 **/    
    public $validate = array(

            'username' => array (
                'rule-1' => array ( 
                    'rule' => 'notEmpty',
                    'message' => 'Please enter Username'
                ),
                'rule-2' => array ( 
                    'rule' => 'isUnique',
                    'message' => 'Please enter other username'
                )
            ),  
            'email' => array (
                'rule-1' => array ( 
                    'rule' => 'notEmpty',
                    'message' => 'Please enter Email'
                ),
                'rule-2' => array ( 
                    'rule' => 'isUnique',
                    'message' => 'Please enter other email'
                ),
                'rule-3' => array ( 
                    'rule' => 'email',
                    'message' => 'Please enter a valid email'
                )
            ), 
            'password' => array(
                'rule-1' => array(
                    'rule' => array('checkForPassword'),  
                    'message' => 'Password must be greater 5 character.'
                )
            ),
            'confirmPassword' => array(
                'rule-1' => array(
                    'rule' => array('identicalFieldValues', 'password'),  
                    'message' => 'Please confirm your password again.'
                )
            )               
        );
/**
 * @method: identicalFieldValues
 * @purpose: to check if 2 values are equal
 * @param $field, $compare_fields
 * @return boolean
 **/
    function identicalFieldValues( $field = array(), $compare_field = null ) {
        foreach( $field as $key => $value ){
            $v1 = $value;
            $v2 = $_POST['data'][$this->name][$compare_field];

            if($v1 != $v2) {
                return false;
            } else {
                continue;
            }
        }
        return true; 
    }
    
/**
 * @method: checkForPassword
 * @purpose: to validate Password
 * @param none
 * @return boolean
 **/
    function checkForPassword() {
    
        $password = $_POST['data'][$this->name]['password'];

        $length = strlen($password);

        if($length < 5) { //Check for Length
            return false;
        } else {
            return true;  
        }
    }
    
    
    public static function  get_role($role_id){
        $role = '';
        switch ($role_id) {
            case self::ADMINISTRATOR:
                $role = 'Administrator';
                return $role;
                break;
            case self::MOD:
                $role = 'Coordinator';
                return $role;
                break;
        }
    }	
    public static function  get_status($status){
        $role = '';
        switch ($status) {
            case 0:
                $role = 'Inactive';
                return $role;
                break;
            case 1:
                $role = 'Active';
                return $role;
                break;
        }
    }	
    public function get_role_list(){
        return array(self::ADMINISTRATOR => 'Administrator', self::COORDINATOR =>'Coordinator',  self::READONLY => 'Read Only' , self::CUSTOM =>'Custom');
    }
    public function beforeSave($options = array()) {
      if(isset($this->data['User']['password']))
        $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
        return true;
    }
    public function get_emsadmin(){
        return $this->find("all",array('conditions'=>array('role_id'=>  self::ADMINISTRATOR,'status'=>1)));
    }
    public static function get_username($id) {
        $user = ClassRegistry::init('User');
        $user = $user->findById($id);
        if(!empty($user)){
            return $user['User']['username'];
        }
        return '';
    }
}